package com.daocao.hua.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.daocao.hua.R;
import com.daocao.hua.adapter.MyExtensionAdapter;
import com.daocao.hua.adapter.TabTopAdapter;
import com.daocao.hua.api.Api;
import com.daocao.hua.api.bean.SpreadListsBean;
import com.daocao.hua.util.Network;
import com.daocao.hua.util.OnLoadeMoreListener;
import com.daocao.hua.util.RxLifeUtil;
import com.daocao.hua.util.ToastUtil;
import com.daocao.hua.util.Util;
import com.daocao.hua.view.V19FrameLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class MyExtensionActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.tablayout)
    RecyclerView tablayout;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    private Unbinder unbinder;
    private TabTopAdapter TabAdapter;
    private MyExtensionAdapter adapter;
    private String status = "1";
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getMySpreadLists(page);
        }
    };
    private boolean is_shenhe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_extension);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("我的推广");
        is_shenhe = getIntent().getBooleanExtra("is_shenhe", false);


        TabAdapter = new TabTopAdapter(this);
        TabAdapter.setOnChooseListener(new TabTopAdapter.OnChooseListener() {
            @Override
            public void onClick(int poi) {

                if (poi == 0) {
                    status = "1";
                } else if (poi == 1) {
                    status = "0";
                } else if (poi == 2) {
                    status = "2";
                }
                adapter.setStatus(status);
                onLoadeMoreListener.setPage();
                getMySpreadLists(1);
            }
        });
        tablayout.setLayoutManager(new GridLayoutManager(this, 3));
        tablayout.setAdapter(TabAdapter);
        List<String> tab_titles = new ArrayList<>();
        tab_titles.add("审核成功");
        tab_titles.add("审核中");
        tab_titles.add("审核失败");
        TabAdapter.update(tab_titles);

        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onLoadeMoreListener.setPage();
                getMySpreadLists(1);
            }
        });
        adapter = new MyExtensionAdapter(this);
        list.addOnScrollListener(onLoadeMoreListener);
        list.setAdapter(adapter);

        if (is_shenhe) {
            status = "0";
            adapter.setStatus(status);
            TabAdapter.setSelectPoi(1);
        }


        getMySpreadLists(1);
    }

    public void getMySpreadLists(int page) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        SpreadListsBean bean = new SpreadListsBean();
        bean.page = page;
        bean.status = status;

        Api.api_service.getMySpreadLists(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.data.lists, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                        if (page == 1) {
                            list.smoothScrollToPosition(0);
                        }
                    }
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        Util.startActivity(this, EarnMoneyActivity.class);
        finish();
    }
}
